package com.bong.panpan.bongcharttools.fragment;

import com.bong.panpan.bongcharttools.custom.Constant;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by panpan on 2017/9/29.
 */

public class ChartListContents {
    /**
     * An array of sample (dummy) items.
     */
    public static final List<ChartListItem> ITEMS = new ArrayList<>();

    public static void RefreshContents(String dirPath) {
        RefreshContents(dirPath, Constant.chartEndName);
    }

    public static void RefreshContents(String dirPath, final String endName) {
        ITEMS.clear();
        //ITEM_MAP.clear();
        File file = new File(dirPath);
        if (file.exists() && file.isDirectory()) {
            File[] files = file.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(endName);
                }
            });
            File temp;
            String name;
            for (int i = 0; i < files.length; i++) {
                temp = files[i];
                name = temp.getName();
                name = name.substring(0, name.lastIndexOf('.'));
                ChartListItem item = new ChartListItem(name, null, temp.getPath(), temp.lastModified());
                addItem(item);
            }
        }
    }

    private static void addItem(ChartListItem item) {
        ITEMS.add(item);
        //ITEM_MAP.put(item.id, item);
    }


    private static String makeDetails(int position) {
        StringBuilder builder = new StringBuilder();
        builder.append("Details about Item: ").append(position);
        for (int i = 0; i < position; i++) {
            builder.append("\nMore details information here.");
        }
        return builder.toString();
    }

    /**
     * A dummy item representing a piece of content.
     */
    public static class ChartListItem {
        public final String content;
        public final String details;
        public final long contentDate;
        public final String contentPath;

        public ChartListItem(String content, String details, String contentPath, long contentDate) {
            this.content = content;
            this.details = details;
            this.contentPath = contentPath;
            this.contentDate = contentDate;
        }

        @Override
        public String toString() {
            return content;
        }
    }
}
